package com.slofzx.nuoda.evaluation.param.service.impl;

import com.slofzx.nuoda.evaluation.param.dao.CommonUnitCostDAO;
import com.slofzx.nuoda.evaluation.param.dto.CommonUnitCostDTO;
import com.slofzx.nuoda.evaluation.param.entity.CommonUnitCost;
import com.slofzx.nuoda.evaluation.param.entity.ProjectCostData;
import com.slofzx.nuoda.evaluation.param.dto.ProjectCostDataDTO;
import com.slofzx.nuoda.evaluation.param.vo.ProjectCostDataVO;
import com.slofzx.nuoda.evaluation.param.dao.ProjectCostDataDAO;
import com.slofzx.nuoda.evaluation.param.service.ProjectCostDataService;
import com.slofzx.nuoda.evaluation.project.observer.ProjectObserver;
import com.slofzx.nuoda.evaluation.project.util.CglibUtils;
import com.slofzx.nuoda.evaluation.project.vo.ProjectInfoVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author nuoda
 * @since 2024-05-20 10:43:07
 */
@Service("projectCostDataService")
public class ProjectCostDataServiceImpl extends BaseServiceImpl implements ProjectCostDataService, ProjectObserver {
    @Autowired
    private ProjectCostDataDAO projectCostDataDAO;
    @Autowired
    private CommonUnitCostDAO commonUnitCostDAO;

    /**
     * 将数据表数据转换为返回数据类型
     *
     * @param entity 数据表对应类
     * @return 实例对象
     */
    protected ProjectCostDataVO parseEntity2VO(ProjectCostData entity) {
        ProjectCostDataVO vo = new ProjectCostDataVO();
        vo.setId(entity.getId());
        vo.setOperateCostStandard(entity.getOperateCostStandard());
        vo.setMiningOperationPrice(entity.getMiningOperationPrice());
        vo.setWaterInjectionPrice(entity.getWaterInjectionPrice());
        vo.setHeavyOilThRecPrice(entity.getHeavyOilThRecPrice());
        vo.setOilGasProcessPrice(entity.getOilGasProcessPrice());
        vo.setCondensateRecyclePrice(entity.getCondensateRecyclePrice());
        vo.setUnderWellOperationPrice(entity.getUnderWellOperationPrice());
        vo.setWellTestPrice(entity.getWellTestPrice());
        vo.setGasPurificationPrice(entity.getGasPurificationPrice());
        vo.setRepairPrice(entity.getRepairPrice());
        vo.setTransferPrice(entity.getTransferPrice());
        vo.setOtherAuxiliaryPrice(entity.getOtherAuxiliaryPrice());
        vo.setFactoryMineManagePrice(entity.getFactoryMineManagePrice());
        vo.setOilQuotaStandard(entity.getOilQuotaStandard());
        vo.setFixedCost(entity.getFixedCost());
        vo.setVariableCost(entity.getVariableCost());
        vo.setPotionAmortisePeriod(entity.getPotionAmortisePeriod());
        vo.setIaAmortisePeriod(entity.getIaAmortisePeriod());
        vo.setOtherAssetsAmortisePeriod(entity.getOtherAssetsAmortisePeriod());
        vo.setLoanRepaymentPeriod(entity.getLoanRepaymentPeriod());
        vo.setFixedAssetBorrowRatio(entity.getFixedAssetBorrowRatio());
        vo.setDisposalFeeRatio(entity.getDisposalFeeRatio());
        vo.setLongTermLoanInterestRate(entity.getLongTermLoanInterestRate());
        vo.setLiquidBorrowInterestRate(entity.getLiquidBorrowInterestRate());
        vo.setLiquidBorrowInterestRatio(entity.getLiquidBorrowInterestRatio());
        vo.setLiquidOfBusinessRatio(entity.getLiquidOfBusinessRatio());
        vo.setCostInputTaxOfOptRatio(entity.getCostInputTaxOfOptRatio());
        vo.setUnitSellCost(entity.getUnitSellCost());
        vo.setSurplusReserveRatio(entity.getSurplusReserveRatio());
        vo.setExchangeRate(entity.getExchangeRate());
        vo.setTonToBarrelRatio(entity.getTonToBarrelRatio());
        vo.setOilGasEquivalent(entity.getOilGasEquivalent());
        vo.setCondensateEquivalent(entity.getCondensateEquivalent());
        vo.setOtherManageCostGround(entity.getOtherManageCostGround());
        vo.setMineralResourcesComprate(entity.getMineralResourcesComprate());
        vo.setBusinessCostRatio(entity.getBusinessCostRatio());
        vo.setSafetyCostRatioOil(entity.getSafetyCostRatioOil());
        vo.setSafetyCostRatioGas(entity.getSafetyCostRatioGas());
        vo.setOilValueAddedTaxRate(entity.getOilValueAddedTaxRate());
        vo.setGasValueAddedTaxRate(entity.getGasValueAddedTaxRate());
        vo.setOtherValueAddedTaxRate(entity.getOtherValueAddedTaxRate());
        vo.setCityConstructionTaxRate(entity.getCityConstructionTaxRate());
        vo.setEducationSurchargeRate(entity.getEducationSurchargeRate());
        vo.setResourceTaxRate(entity.getResourceTaxRate());
        vo.setIncomeTaxRate(entity.getIncomeTaxRate());
        vo.setOtherManageCostConventional(entity.getOtherManageCostConventional());
        vo.setOtherManageCostDeApportion(entity.getOtherManageCostDeApportion());
        vo.setGasManageCostConventional(entity.getGasManageCostConventional());
        vo.setGasManageCostDeApportion(entity.getGasManageCostDeApportion());
        vo.setGasManageCostGround(entity.getGasManageCostGround());
        return vo;
    }

    /**
     * 将数据传递类转换为数据表数据类
     *
     * @param dto 数据传递类实例
     * @return 实例对象
     */
    protected ProjectCostData parseDTO2Entity(ProjectCostDataDTO dto) {
        ProjectCostData entity = new ProjectCostData();
        entity.setId(dto.getId());
        entity.setOperateCostStandard(dto.getOperateCostStandard());
        entity.setMiningOperationPrice(dto.getMiningOperationPrice());
        entity.setWaterInjectionPrice(dto.getWaterInjectionPrice());
        entity.setHeavyOilThRecPrice(dto.getHeavyOilThRecPrice());
        entity.setOilGasProcessPrice(dto.getOilGasProcessPrice());
        entity.setCondensateRecyclePrice(dto.getCondensateRecyclePrice());
        entity.setUnderWellOperationPrice(dto.getUnderWellOperationPrice());
        entity.setWellTestPrice(dto.getWellTestPrice());
        entity.setGasPurificationPrice(dto.getGasPurificationPrice());
        entity.setRepairPrice(dto.getRepairPrice());
        entity.setTransferPrice(dto.getTransferPrice());
        entity.setOtherAuxiliaryPrice(dto.getOtherAuxiliaryPrice());
        entity.setFactoryMineManagePrice(dto.getFactoryMineManagePrice());
        entity.setOilQuotaStandard(dto.getOilQuotaStandard());
        entity.setFixedCost(dto.getFixedCost());
        entity.setVariableCost(dto.getVariableCost());
        entity.setPotionAmortisePeriod(dto.getPotionAmortisePeriod());
        entity.setIaAmortisePeriod(dto.getIaAmortisePeriod());
        entity.setOtherAssetsAmortisePeriod(dto.getOtherAssetsAmortisePeriod());
        entity.setLoanRepaymentPeriod(dto.getLoanRepaymentPeriod());
        entity.setFixedAssetBorrowRatio(dto.getFixedAssetBorrowRatio());
        entity.setDisposalFeeRatio(dto.getDisposalFeeRatio());
        entity.setLongTermLoanInterestRate(dto.getLongTermLoanInterestRate());
        entity.setLiquidBorrowInterestRate(dto.getLiquidBorrowInterestRate());
        entity.setLiquidBorrowInterestRatio(dto.getLiquidBorrowInterestRatio());
        entity.setLiquidOfBusinessRatio(dto.getLiquidOfBusinessRatio());
        entity.setCostInputTaxOfOptRatio(dto.getCostInputTaxOfOptRatio());
        entity.setUnitSellCost(dto.getUnitSellCost());
        entity.setSurplusReserveRatio(dto.getSurplusReserveRatio());
        entity.setExchangeRate(dto.getExchangeRate());
        entity.setTonToBarrelRatio(dto.getTonToBarrelRatio());
        entity.setOilGasEquivalent(dto.getOilGasEquivalent());
        entity.setCondensateEquivalent(dto.getCondensateEquivalent());
        entity.setOtherManageCostGround(dto.getOtherManageCostGround());
        entity.setMineralResourcesComprate(dto.getMineralResourcesComprate());
        entity.setBusinessCostRatio(dto.getBusinessCostRatio());
        entity.setSafetyCostRatioOil(dto.getSafetyCostRatioOil());
        entity.setSafetyCostRatioGas(dto.getSafetyCostRatioGas());
        entity.setOilValueAddedTaxRate(dto.getOilValueAddedTaxRate());
        entity.setGasValueAddedTaxRate(dto.getGasValueAddedTaxRate());
        entity.setOtherValueAddedTaxRate(dto.getOtherValueAddedTaxRate());
        entity.setCityConstructionTaxRate(dto.getCityConstructionTaxRate());
        entity.setEducationSurchargeRate(dto.getEducationSurchargeRate());
        entity.setResourceTaxRate(dto.getResourceTaxRate());
        entity.setIncomeTaxRate(dto.getIncomeTaxRate());
        entity.setOtherManageCostConventional(dto.getOtherManageCostConventional());
        entity.setOtherManageCostDeApportion(dto.getOtherManageCostDeApportion());
        entity.setGasManageCostConventional(dto.getGasManageCostConventional());
        entity.setGasManageCostDeApportion(dto.getGasManageCostDeApportion());
        entity.setGasManageCostGround(dto.getGasManageCostGround());
        return entity;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProjectCostDataVO queryById(String id) {
        ProjectCostData entity = this.projectCostDataDAO.selectById(id);
        if (entity == null) {
            return null;
        }
        return parseEntity2VO(entity);
    }

    /**
     * 查询多条数据
     *
     * @param dto 数据传递类实例对象
     * @return 对象列表
     */
    @Override
    public List<ProjectCostDataVO> queryList(ProjectCostDataDTO dto) {
        //mybatis-plus 写法示例
        //IPage<ProjectCostData> pageResult = this.evaModelDAO.selectPage(new Page<>(dto.getPageNo(),dto.getPageSize()),null);
        //List<ProjectCostData> dataList = pageResult.getRecords();
        //dto.setCount((int) pageResult.getTotal());
        List<ProjectCostData> dataList = this.projectCostDataDAO.queryList(dto);
        if (null == dataList || dataList.isEmpty()) {
            return Collections.emptyList();
        }
        List<ProjectCostDataVO> resultList = new ArrayList(dataList.size());
        for (ProjectCostData item : dataList) {
            ProjectCostDataVO vo = parseEntity2VO(item);
            resultList.add(vo);
        }
        // TODO 添加其他关联查询数据
        return resultList;
    }

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult create(ProjectCostDataDTO dto) {
        // TODO 添加校验逻辑
        ProjectCostData entity = parseDTO2Entity(dto);
       // preInsert(entity);
        this.projectCostDataDAO.insert(entity);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public ServiceResult batchSave(List<ProjectCostDataDTO> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return CommonResultEnum.SUCCESS;
        }
        // 删除老的数据
        final Set<String> collect = dtos.stream().map(ProjectCostDataDTO::getId).collect(Collectors.toSet());
        projectCostDataDAO.deleteBatchIds(collect);
        List<ProjectCostData> list = new ArrayList<>();
        for (ProjectCostDataDTO dto : dtos) {
            final ProjectCostData projectCostData = parseDTO2Entity(dto);
            list.add(projectCostData);
        }
        saveBatch(list,projectCostDataDAO,200);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult update(ProjectCostDataDTO dto) {
        String id = dto.getId();
        if (StringUtils.isEmpty(id)) {
            return CommonResultEnum.NOT_EXIST;
        }
        ProjectCostData entity = this.projectCostDataDAO.selectById(id);
        if (entity == null) {
            return CommonResultEnum.NOT_EXIST;
        }
        // TODO 添加其他校验逻辑，如数据权限

        entity.setOperateCostStandard(dto.getOperateCostStandard());
        entity.setMiningOperationPrice(dto.getMiningOperationPrice());
        entity.setWaterInjectionPrice(dto.getWaterInjectionPrice());
        entity.setHeavyOilThRecPrice(dto.getHeavyOilThRecPrice());
        entity.setOilGasProcessPrice(dto.getOilGasProcessPrice());
        entity.setCondensateRecyclePrice(dto.getCondensateRecyclePrice());
        entity.setUnderWellOperationPrice(dto.getUnderWellOperationPrice());
        entity.setWellTestPrice(dto.getWellTestPrice());
        entity.setGasPurificationPrice(dto.getGasPurificationPrice());
        entity.setRepairPrice(dto.getRepairPrice());
        entity.setTransferPrice(dto.getTransferPrice());
        entity.setOtherAuxiliaryPrice(dto.getOtherAuxiliaryPrice());
        entity.setFactoryMineManagePrice(dto.getFactoryMineManagePrice());
        entity.setOilQuotaStandard(dto.getOilQuotaStandard());
        entity.setFixedCost(dto.getFixedCost());
        entity.setVariableCost(dto.getVariableCost());
        entity.setPotionAmortisePeriod(dto.getPotionAmortisePeriod());
        entity.setIaAmortisePeriod(dto.getIaAmortisePeriod());
        entity.setOtherAssetsAmortisePeriod(dto.getOtherAssetsAmortisePeriod());
        entity.setLoanRepaymentPeriod(dto.getLoanRepaymentPeriod());
        entity.setFixedAssetBorrowRatio(dto.getFixedAssetBorrowRatio());
        entity.setDisposalFeeRatio(dto.getDisposalFeeRatio());
        entity.setLongTermLoanInterestRate(dto.getLongTermLoanInterestRate());
        entity.setLiquidBorrowInterestRate(dto.getLiquidBorrowInterestRate());
        entity.setLiquidBorrowInterestRatio(dto.getLiquidBorrowInterestRatio());
        entity.setLiquidOfBusinessRatio(dto.getLiquidOfBusinessRatio());
        entity.setCostInputTaxOfOptRatio(dto.getCostInputTaxOfOptRatio());
        entity.setUnitSellCost(dto.getUnitSellCost());
        entity.setSurplusReserveRatio(dto.getSurplusReserveRatio());
        entity.setExchangeRate(dto.getExchangeRate());
        entity.setTonToBarrelRatio(dto.getTonToBarrelRatio());
        entity.setOilGasEquivalent(dto.getOilGasEquivalent());
        entity.setCondensateEquivalent(dto.getCondensateEquivalent());
        entity.setOtherManageCostGround(dto.getOtherManageCostGround());
        entity.setMineralResourcesComprate(dto.getMineralResourcesComprate());
        entity.setBusinessCostRatio(dto.getBusinessCostRatio());
        entity.setSafetyCostRatioOil(dto.getSafetyCostRatioOil());
        entity.setSafetyCostRatioGas(dto.getSafetyCostRatioGas());
        entity.setOilValueAddedTaxRate(dto.getOilValueAddedTaxRate());
        entity.setGasValueAddedTaxRate(dto.getGasValueAddedTaxRate());
        entity.setOtherValueAddedTaxRate(dto.getOtherValueAddedTaxRate());
        entity.setCityConstructionTaxRate(dto.getCityConstructionTaxRate());
        entity.setEducationSurchargeRate(dto.getEducationSurchargeRate());
        entity.setResourceTaxRate(dto.getResourceTaxRate());
        entity.setIncomeTaxRate(dto.getIncomeTaxRate());
        entity.setOtherManageCostConventional(dto.getOtherManageCostConventional());
        entity.setOtherManageCostDeApportion(dto.getOtherManageCostDeApportion());
        entity.setGasManageCostConventional(dto.getGasManageCostConventional());
        entity.setGasManageCostDeApportion(dto.getGasManageCostDeApportion());
        entity.setGasManageCostGround(dto.getGasManageCostGround());
        this.projectCostDataDAO.updateById(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 操作结果
     */
    @Override
    public ServiceResult deleteById(String id) {
        // TODO 添加校验逻辑，如数据权限
        this.projectCostDataDAO.deleteById(id);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public ServiceResult deleteByBatchId(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return CommonResultEnum.SUCCESS;
        }
        projectCostDataDAO.deleteBatchIds(ids);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public void updateByParamVersion(String paramVersion, List<ProjectInfoVO> infoVOList) {

        final Map<String,ProjectInfoVO> projectInfoVOMap = infoVOList.stream().collect(Collectors.toMap(ProjectInfoVO::getId,Function.identity(),(v1,v2)->v1));
        final CommonUnitCostDTO unitCostDTO = new CommonUnitCostDTO();
        unitCostDTO.setVersionId(paramVersion);
        final List<CommonUnitCost> commonUnitCosts = commonUnitCostDAO.queryList(unitCostDTO);
        final Map<String, CommonUnitCost> commonUnitCostMap = commonUnitCosts.stream().collect(Collectors.toMap(CommonUnitCost::getCompanyId, Function.identity(), (v1, v2) -> v1));
        final ProjectCostDataDTO costDataDTO = new ProjectCostDataDTO();
        costDataDTO.setFilterIdSet(projectInfoVOMap.keySet());
        final List<ProjectCostData> projectCostDataList = projectCostDataDAO.queryList(costDataDTO);
        for (ProjectCostData projectCostData : projectCostDataList) {
            final ProjectInfoVO vo = projectInfoVOMap.get(projectCostData.getId());
            final CommonUnitCost commonUnitCost = commonUnitCostMap.get(vo.getBuildOffice());
            projectCostData.setOperateCostStandard(commonUnitCost.getOperateCostStandard());
            projectCostData.setMiningOperationPrice(commonUnitCost.getMiningOperationPrice());
            projectCostData.setWaterInjectionPrice(commonUnitCost.getWaterInjectionPrice());
            projectCostData.setHeavyOilThRecPrice(commonUnitCost.getHeavyOilThRecPrice());
            projectCostData.setOilGasProcessPrice(commonUnitCost.getOilGasProcessPrice());
            projectCostData.setCondensateRecyclePrice(commonUnitCost.getCondensateRecyclePrice());
            projectCostData.setUnderWellOperationPrice(commonUnitCost.getUnderWellOperationPrice());
            projectCostData.setWellTestPrice(commonUnitCost.getWellTestPrice());
            projectCostData.setGasPurificationPrice(commonUnitCost.getGasPurificationPrice());
            projectCostData.setRepairPrice(commonUnitCost.getRepairPrice());
            projectCostData.setTransferPrice(commonUnitCost.getTransferPrice());
            projectCostData.setOtherAuxiliaryPrice(commonUnitCost.getOtherAuxiliaryPrice());
            projectCostData.setFactoryMineManagePrice(commonUnitCost.getFactoryMineManagePrice());
            projectCostData.setOilQuotaStandard(commonUnitCost.getOilQuotaStandard());
            projectCostData.setFixedCost(commonUnitCost.getFixedCost());
            projectCostData.setVariableCost(commonUnitCost.getVariableCost());
        }
        updateBatchById(projectCostDataList, projectCostDataDAO, ProjectCostData.class);

    }

    @Override
    public String getName() {
        return "project-cost-data";
    }

    @Override
    public ServiceResult handleProjectCreate(String projectId) {
        // 导入时使用了批量插入这里不在操作
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public ServiceResult handleProjectDelete(String projectId) {
        projectCostDataDAO.deleteById(projectId);
        return CommonResultEnum.SUCCESS;
    }
}
